package com.platform.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.annotation.Authentication;
import com.platform.base.PageUtil;
import com.platform.dao.ModularDAO;
import com.platform.dao.RightDAO;
import com.platform.dto.Modular;
import com.platform.dto.Right;
import com.platform.exception.BizException;
import com.platform.ext.mybatis.annotation.UseDataSource;
import com.platform.service.IModular;
import com.platform.utils.ChineseToEnglish;
import com.platform.utils.StringUtils;

import cn.hutool.core.map.MapUtil;

/**
 * 
 * <p> Title:Mkxx 实现</p>
 * <p> Description:  模块信息</p>
 * <p> Copyright: Copyright (c) 2019 </p>
 * <p> Company: </p>
 *
 * @author wys
 * @version 1.0
 */
@Service

public class IModularImpl  implements IModular  {
	
	@Autowired
	private RightDAO<Right> rightDAO;
	
	@Autowired
    private ModularDAO<Modular> DAO;
        
    public ModularDAO<Modular> getDAO() {
        return DAO;
    }
    
    /** =========================== 数据库操作 ==================================== **/
    /**
     * 
     * selectAll(查询所有) requiredPre="MENU_FIND"
     * (这里描述这个方法适用条件 – 可选)
     * @return 
     * @return List
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication/*(requiredPre="MENUSMANAGER_FIND")*/
    @UseDataSource()
    public List<Modular> selectAll(Map<String,Object> map) {
        List<Modular> s = null;
        try {
            s = DAO.selectByMap(map);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return s;
    }

    /**
     * 
     * selectTotal(查询总条数)
     * (这里描述这个方法适用条件 – 可选)
     * @param map
     * @return 
     * @return Integer
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication/*(requiredPre="MENUSMANAGER_FIND")*/
    @UseDataSource()
    public Integer selectTotal(Map<String, Object> map) {
        Integer total = 0; 
        try {
            total = DAO.selectByMapCount(map);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return total;
    }

    /**
     * 
     * selectByPaging(分页查询)
     * (这里描述这个方法适用条件 – 可选)
     * @param pageUtil
     * @return 
     * @return List<MkxxDTO>
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication/*(requiredPre="MENUSMANAGER_FIND")*/
    @UseDataSource()
    public List<Modular> selectByPaging(PageUtil pageUtil) {
        List<Modular> pagingList = null;
        try {
            pagingList = DAO.selectByMapPaging(pageUtil);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return pagingList;
    }

    /**
     * 
     * delete(删除)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="MENUSMANAGER_DELETED")
    @UseDataSource()
    public boolean delete(Modular dto) {
        boolean flag = false;
        try {
            if(DAO.deleteByPrimaryKey(dto.getId()) > 0) {
            	//删除模块同时，删除权限依赖
            	rightDAO.deleteBySourceCode(dto.getModularcode());
            	flag = true;
            }else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    /**
     * 
     * update(修改)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="MENUSMANAGER_EDIT")
    @UseDataSource()
    public boolean update(Modular dto) {
        boolean flag = false;
        try {
//        	if(!StringUtils.isEmpty(dto.getParent_())){
//	        	Map<String,Object> map = selectNextCode(dto.getParent_());
//	        	dto.setModularcode(map.get("typecodes").toString());
//	            dto.setChildren_(map.get("fathertype").toString());
//        	}
        	dto.setShowname(dto.getName());
            if(DAO.updateBySelective(dto) > 0)
                flag = true;
            else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    /**
     * 
     * insert(新增)
     * (这里描述这个方法适用条件 – 可选)
     * @param dto 
     * @return void
     * @exception 
     * @version  1.0.0
     */
    @Override
    @Authentication(requiredPre="MENUSMANAGER_SAVE")
    @UseDataSource()
    public boolean insert(Modular dto) {
        boolean flag = false;
        try {
        	Map<String,Object> map = selectNextCode(dto.getParent_());
        	dto.setShowname(dto.getName());
        	dto.setModularen(ChineseToEnglish.getPinYinHeadChar(dto.getName()).toUpperCase());
        	dto.setModularcode(map.get("typecodes").toString());
        	dto.setParent_(StringUtils.isEmpty(dto.getParent_())?"0":dto.getParent_());
            dto.setChildren_(map.get("fathertype").toString());
            if(DAO.insert(dto) > 0)
                flag = true;
            else 
                flag = false;
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return flag;
    }

    @Override
    @Authentication/*(requiredPre="MENUSMANAGER_FIND")*/
    @UseDataSource()
    public Modular get(Object code) {
    	Modular dto = null;
        try {
            dto = DAO.selectByPrimaryKey(code);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return dto;
    }

    /** =========================== 存储过程操作 ==================================== **/
    
    /** =========================== 自定义标签调用  ==================================== **/
    @Override
    @Authentication/*(requiredPre="MENUSMANAGER_FIND")*/
    @UseDataSource()
    public List<Modular> selectParent(){
        List<Modular> dtoList = null;
        try {
            dtoList = DAO.selectParent();
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return dtoList;
    }

    @Override
    @Authentication/*(requiredPre="MENUSMANAGER_FIND")*/
    @UseDataSource()
    public List<Modular> selectMKByParent(String fl) {
        List<Modular> dtoList = null;
        try {
            dtoList = DAO.selectMKByParent(fl);
        } catch (BizException e) {
            throw new BizException(e.getMessage());
        }
        return dtoList;
    }
    
    /**
     * 查询最大节点
     * @param fatherType
     * @return
     * @throws BizException
     */
    @Authentication/*(requiredPre="MENUSMANAGER_FIND")*/
    @UseDataSource()
    private Map<String, Object> selectNextCode(String fathertype) throws BizException{
    	if(StringUtils.isEmpty(fathertype)) {//如果为空的话则默认设置0根节点
    		fathertype = "0";
    	}
        Map<String,Object> resultMap = MapUtil.newHashMap();
        String resultLis = DAO.selectNextCode(fathertype);
        if (resultLis == null) {
            resultLis = "001";
        } else if (resultLis.length() == 3) {
            resultLis = String.valueOf(Integer.valueOf(resultLis) + 1);
            switch (resultLis.length()) {
            case 1:
                resultLis = "00" + resultLis;
                break;
            case 2:
                resultLis = "0" + resultLis;
                break;
            default:
                break;
            }

        } else {
            resultLis = resultLis.substring(resultLis.length() - 3,resultLis.length());
            resultLis = String.valueOf(Integer.valueOf(resultLis) + 1);
            switch (resultLis.length()) {
            case 1:
                resultLis = "00" + resultLis;
                break;
            case 2:
                resultLis = "0" + resultLis;
                break;
            default:
                break;
            }
        }
        resultMap.put("fathertype", resultLis);
        resultMap.put("typecodes", (fathertype.equals("0")?"0001002":fathertype)+resultLis);
        return resultMap;
    }

    public static void main(String[] args) {
		String  s = "0001";
		System.out.println(s.substring(1,4));
	}
    
    @Override
    @Authentication
    @UseDataSource()
    public Modular CurrentPosition(String reqPath) {
        try {
        	Modular dto = DAO.CurrentPosition(reqPath);
            return dto;
        } catch (BizException e) {
            e.printStackTrace();
            return null;
        }
    }
    
}